//
//  DXpopButton.m
//  UILbrary
//
//  Created by mac on 16/7/19.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "DXpopButton.h"
#import "Constant.h"
#import "BaseButton.h"

static CGFloat const buttonSize = 80;
static CGFloat const bottomHeith = 200;
static NSTimeInterval const kAnimalPopAndDismmisTime = 0.25;
@interface DXpopButton ()
@property (nonatomic, strong) UIView *bottomBgView;
@property (nonatomic, strong) UIView *topBgView;
@property (nonatomic, strong) NSArray *topImageArr;
@property (nonatomic, strong) NSArray *bottomImageArr;
@end
@implementation DXpopButton
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        [self createSubViews];
    }
    return self;
}


- (instancetype)initWithFrame:(CGRect )frma TopimageArr:(NSArray *)topImagearr bottomImageArr:(NSArray *)bottomArr{
    
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds]) {
        self.bottomImageArr = topImagearr;
        self.topImageArr = topImagearr;
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    
    [self addSubview:self.topBgView];
    [self addSubview:self.bottomBgView];
    
    [self addTopSubviews];
    [self addBottomSubviews];
    
    }

- (void)addTopSubviews{
    __weak typeof(self) weakSelf = self;
    for (int i=0; i<6; i++) {
        CGFloat buttonOrignX = (kAPP_With - 3*buttonSize)/4.0 + (buttonSize + (kAPP_With - 3*buttonSize)/4.0) * (i % 3);
        CGFloat buttonOrinY = (bottomHeith - 2*buttonSize)/3.0 + ((bottomHeith - 2*buttonSize)/3.0 + buttonSize) * (i/3);
        
        
        BaseButton *button = [[BaseButton alloc] initWithFrame:CGRectMake(buttonOrignX, buttonOrinY, buttonSize, buttonSize)];
        button.backgroundColor = [UIColor orangeColor];
        [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.topImageArr[i]]] forState:UIControlStateNormal];
        [button setTitle:@"启动" forState:UIControlStateNormal];
        
        button.isTopImageBottomTitle = YES;
        [self.topBgView addSubview:button];
        [button bk_addEventHandler:^(id sender) {
            weakSelf.topButtonsClick(i,button.titleLabel.text);
        } forControlEvents:UIControlEventTouchUpInside];
        
    }

}

- (void)addBottomSubviews{
    
    __weak typeof(self) weakSelf=  self;
    for (int i=0; i<6; i++) {
        CGFloat buttonOrignX = (kAPP_With - 3*buttonSize)/4.0 + (buttonSize + (kAPP_With - 3*buttonSize)/4.0) * (i % 3);
        CGFloat buttonOrinY = (bottomHeith - 2*buttonSize)/3.0 + ((bottomHeith - 2*buttonSize)/3.0 + buttonSize) * (i/3);
        
        
        if (i==1 || i==3 || i==5) {
            BaseButton *button = [[BaseButton alloc] initWithFrame:CGRectMake(buttonOrignX, buttonOrinY, buttonSize, buttonSize)];
            button.backgroundColor = [UIColor orangeColor];
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",self.bottomImageArr[i]]] forState:UIControlStateNormal];
            [button setTitle:@"启动" forState:UIControlStateNormal];
            
                    button.isTopImageBottomTitle = YES;
            [self.bottomBgView addSubview:button];
            
           __block bool isPop = NO;
            [button bk_addEventHandler:^(id sender) {
                if (i==1) {
                    isPop = !isPop;
                    if (isPop) {
                        [self popTopButtons];
                    }else{
                        [self dismissTopView];
                    }
                    
                }
                weakSelf.bottomButtonsClick(i,button.titleLabel.text);
            } forControlEvents:UIControlEventTouchUpInside];
        }
        
        
    }

    
    
}

- (void)popTopButtons{
    [UIView animateWithDuration:kAnimalPopAndDismmisTime animations:^{
        self.topBgView.frame = CGRectMake(0, kAPP_Hight-bottomHeith*2, kAPP_With, bottomHeith);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)popBottomButtons{
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:self];
    [UIView animateWithDuration:kAnimalPopAndDismmisTime animations:^{
        self.bottomBgView.frame = CGRectMake(0, kAPP_Hight - bottomHeith, kAPP_With, bottomHeith);
        self.topBgView.frame = self.bottomBgView.frame;
    } completion:^(BOOL finished) {
        
    }];
}


- (void)dismissTopView{
    [UIView animateWithDuration:kAnimalPopAndDismmisTime animations:^{
        self.topBgView.frame = self.bottomBgView.frame;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)dismiss{
    [UIView animateWithDuration:kAnimalPopAndDismmisTime animations:^{
        self.topBgView.frame = CGRectMake(0, kAPP_Hight, kAPP_With, 0);
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:kAnimalPopAndDismmisTime animations:^{
            self.bottomBgView.frame = CGRectMake(0, kAPP_Hight, kAPP_With, 0);
        } completion:^(BOOL finished) {
            for (UIView *view in self.bottomBgView.subviews) {
                [view removeFromSuperview];
            }
            [self.bottomBgView removeFromSuperview];
            [self removeFromSuperview];
            self.bottomBgView = nil;
        }];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //点击topview和bottomview区域之外才dismiss
    if (![touches.anyObject.view isEqual:self.bottomBgView] && ![touches.anyObject.view isEqual:self.topBgView]) {
        [self dismiss];
    }
}

- (UIView *)bottomBgView{
    if (!_bottomBgView) {
        _bottomBgView = ({
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, kAPP_Hight, kAPP_With, bottomHeith)];
            view.backgroundColor = [UIColor purpleColor];
            view;
        });
    }
    return  _bottomBgView;
}


- (UIView *)topBgView{
    if (!_topBgView) {
        _topBgView = ({
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, kAPP_Hight,  kAPP_With, bottomHeith)];
            view.backgroundColor = [UIColor whiteColor];
            view;

        });
    }
    return _topBgView;
}
@end
